package math;

/**
 * 
 * @author Ryan Hill
 */
public class Vector2D
{
	
	private double ang, mag;
	
	
	public static Vector2D add(Vector2D v1, Vector2D v2)
	{
		//create a new vector
		Vector2D result = new Vector2D();
		//get the x and y components and add them together
		double x = v1.getXLen() + v2.getXLen();
		double y = v1.getYLen() + v2.getYLen();
		//find the new angle based on the components sum
		result.setAngle(Math.atan2(y, x));
		//find the new magnitude based on the components sum
		result.setMagnitude(Equations.pythagorean(x, y, 'C'));
		return result;
	}
	
	public static Vector2D subtract(Vector2D v1, Vector2D v2)
	{
		Vector2D result = new Vector2D();
		double x = v1.getXLen() - v2.getXLen();
		double y = v1.getYLen() - v2.getYLen();
		result.setAngle(Math.atan2(y, x));
		result.setMagnitude(Equations.pythagorean(x, y, 'C'));
		return result;
	}
	
	public static Vector2D multiply(Vector2D v1, double d)
	{
		Vector2D result = new Vector2D();
		result.setMagnitude(v1.getMagnitude() * d);
		if (d < 0)
		{
			result.setAngle(Equations.normalizeAngle(v1.getAngle()) + 180);
		}
		else
		{
			result.setAngle(v1.getAngle());
		}
		return result;
	}
	
	
	/*public static Vector2D getNormal(Vector2D v1)
	{
		Vector2D result = new Vector2D();
		
		
		double len = v1.getMagnitude();
		
		if(len > 0)
		{
			result.x = v1.x / len;
			result.y = v1.y / len;
		}
		
		return result;
	}*/
	
	public static double dotProduct(Vector2D v1, Vector2D v2)
	{
		return v1.getXLen()*v2.getXLen() + v1.getYLen()*v2.getYLen();
	}
	
	public Vector2D()
	{
		mag = ang = 0;
	}
	
	public Vector2D(double mag, double radians)
	{
		this.mag = mag;
		this.ang = radians;
	}
	
	public Vector2D(Vector2D v)
	{
		setAngle(v.getAngle());
		setMagnitude(v.getMagnitude());
	}
	
	public Vector2D add(Vector2D v)
	{
		double x = getXLen() + v.getXLen();
		double y = getYLen() + v.getYLen();
		setAngle(Equations.normalizeAngle(Math.atan2(y, x)));
		setMagnitude(Equations.pythagorean(x, y, 'C'));
		return this;
	}
	
	public Vector2D subtract(Vector2D v)
	{
		double x = getXLen() - v.getXLen();
		double y = getYLen() - v.getYLen();
		
		setAngle(Equations.normalizeAngle(Math.atan2(y, x)));
		System.out.println(Math.toDegrees(getAngle()));
		setMagnitude(Equations.pythagorean(x, y, 'C'));
		return this;
	}
	
	public Vector2D multiply(double d)
	{
		mag *= d;
		if (d < 0)
		{
			Equations.normalizeAngle(ang + 180);
		}
		return this;
	}
	
	public Vector2D multiply(Vector2D v)
	{
		double x = getXLen() * v.getXLen();
		double y = getYLen() * v.getYLen();
		mag = Equations.pythagorean(x, y, 'C');
		ang = Math.atan2(y, x);
		return this;
	}
	
	//approach zero by an amount
	/*public void approachZero(double amt)
	{
		if (amt > Math.abs(x))
			x = 0;
		if (amt > Math.abs(y))
			y = 0;
		if (x < 0)
			x += amt;
		else if (x > 0)
			x -= amt;
		if (y < 0)
			y += amt;
		else if (y > 0)
			y -= amt;
	}*/
	
	public double dotProduct(Vector2D v)
	{
		return getXLen()*v.getXLen() + getYLen()*v.getYLen();
	}

	public void setAngle(double radians)
	{
		Equations.normalizeAngle(radians);
		ang = radians;
	}
	public double getAngle()
	{
		return ang;
	}
	public void setMagnitude(double mag)
	{
		this.mag = mag;
	}
	public double getMagnitude()
	{
		return mag;
	}
	public double getXLen()
	{
		return mag * Math.cos(ang);
	}
	public double getYLen()
	{
		return mag * Math.sin(ang);
	}
	public String toString()
	{
		return "Angle: " + ang + ", Mag: " + mag;
	}
}
